package com.sys.gradepaysys.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
//基础的数据库配置 使用了c3p0连接池 获取链接

public class DBUtil {
	private static String user = "root";
	private static String password = "200612";
	private static String url = "jdbc:mysql://localhost:3306/classpay?serverTimezone=UTC";
	private static String driver = "com.mysql.jdbc.Driver";
	//使用C3P0数据库连接池的方式，获取数据库的连接：
	private static ComboPooledDataSource ds=null;
	//静态加载一个链接池
	static{
		ds=new ComboPooledDataSource();
	}
	public static Connection getConnection() {
		Connection conn=null;
		try {
			//c3p0链接池配置
			ds.setDriverClass(driver);
			ds.setUser(user);
			ds.setPassword(password);
			ds.setJdbcUrl(url);
			ds.setAcquireIncrement(5);
			ds.setInitialPoolSize(10);
			ds.setMinPoolSize(5);
			ds.setMaxPoolSize(100);
			ds.setMaxStatements(20);
			ds.setMaxStatementsPerConnection(100);
			conn = ds.getConnection();
		}catch (Exception exception){
		   exception.printStackTrace();
		}
		return conn;
	}
	public static void release(Connection connection ,PreparedStatement preparedStatement) {
		try {
			if (connection != null) {
				connection.close();
			}
			if (preparedStatement != null) {
				preparedStatement.close();
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public static void release(Connection connection ,PreparedStatement preparedStatement ,ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		release(connection, preparedStatement);
	}

}
